/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Haystack Software Inc. All rights reserved.
 *  Licensed under the PolyForm Strict License 1.0.0. See License.txt in the project root for
 *  license information.
 *--------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See code-license.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { Event } from "vs/base/common/event"
import { INativeEnvironmentService } from "vs/platform/environment/common/environment"
import { IFileService } from "vs/platform/files/common/files"
import { refineServiceDecorator } from "vs/platform/instantiation/common/instantiation"
import { ILogService } from "vs/platform/log/common/log"
import { IUriIdentityService } from "vs/platform/uriIdentity/common/uriIdentity"
import {
  IUserDataProfilesService,
  WillCreateProfileEvent,
  WillRemoveProfileEvent,
  IUserDataProfile,
} from "vs/platform/userDataProfile/common/userDataProfile"
import { UserDataProfilesService } from "vs/platform/userDataProfile/node/userDataProfile"
import {
  IAnyWorkspaceIdentifier,
  IEmptyWorkspaceIdentifier,
} from "vs/platform/workspace/common/workspace"
import { IStateService } from "vs/platform/state/node/state"

export const IUserDataProfilesMainService = refineServiceDecorator<
  IUserDataProfilesService,
  IUserDataProfilesMainService
>(IUserDataProfilesService)
export interface IUserDataProfilesMainService extends IUserDataProfilesService {
  getProfileForWorkspace(
    workspaceIdentifier: IAnyWorkspaceIdentifier
  ): IUserDataProfile | undefined
  unsetWorkspace(
    workspaceIdentifier: IAnyWorkspaceIdentifier,
    transient?: boolean
  ): void
  getAssociatedEmptyWindows(): IEmptyWorkspaceIdentifier[]
  readonly onWillCreateProfile: Event<WillCreateProfileEvent>
  readonly onWillRemoveProfile: Event<WillRemoveProfileEvent>
}

export class UserDataProfilesMainService
  extends UserDataProfilesService
  implements IUserDataProfilesMainService
{
  constructor(
    @IStateService stateService: IStateService,
    @IUriIdentityService uriIdentityService: IUriIdentityService,
    @INativeEnvironmentService environmentService: INativeEnvironmentService,
    @IFileService fileService: IFileService,
    @ILogService logService: ILogService
  ) {
    super(
      stateService,
      uriIdentityService,
      environmentService,
      fileService,
      logService
    )
  }

  getAssociatedEmptyWindows(): IEmptyWorkspaceIdentifier[] {
    const emptyWindows: IEmptyWorkspaceIdentifier[] = []
    for (const id of this.profilesObject.emptyWindows.keys()) {
      emptyWindows.push({ id })
    }
    return emptyWindows
  }
}
